package controller;

import com.alibaba.dubbo.config.annotation.Reference;
import entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: lagou-dubbo
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-07-27 18:16
 * @Description: 控制层
 */
@Controller
@RequestMapping("/user")
public class UserAction {

    @Reference//远程注入
    private UserService userService;

    @RequestMapping("/register")
    @ResponseBody
    public void register(@RequestBody Users user) {
        System.out.println("来注册");
        try {
            String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            user.setCreatetime(nowTime);
            userService.register(user);
//            return "注册成功！";
        } catch (Exception e) {
            e.printStackTrace();
//            return "失败";
        }
    }

    @RequestMapping("/findAllUsers")
    @ResponseBody
    public List<Users> findAllUsers() {
        System.out.println("加载所有用户");
        try {
            return userService.findAllUsers();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/modifyUser")
    @ResponseBody
    public void modifyUser(@RequestBody Users users) {
        System.out.println("修改用户");
        try {
            userService.modifyUser(users);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public void deleteUser(int uid) {
        System.out.println("删除用户");
        try {
            userService.deleteUser(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/findUsersByName")
    @ResponseBody
    public List<Users> findUsersByName(String string){
        System.out.println("模糊查询");
        try {
            return userService.findUsersByName(string);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
